package com.mobile.check.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

import cn.droidlover.xdroidmvp.kit.Kits;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import com.mobile.check.app.R;


/**
 * Created by Administrator on 2017/4/18.
 */

public class CheckTip2Dialog extends Dialog implements DialogInterface.OnDismissListener {
    TextView receiveTv;
    TextView maxTv;
    TextView minStopTimeTv;
    private TextView submit_tv;
//    Disposable disposable;
    Disposable timerDisposable;
    SlectClickListener mSlectClickListener;

    public CheckTip2Dialog(Context context, SlectClickListener listener) {
        super(context, R.style.WinDialog);
        mSlectClickListener = listener;
        setContentView(R.layout.dialog_checktip);
        init();
    }


    public void display(int minStopTime) {
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER_VERTICAL);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 0;//设置Dialog距离底部的距离
//       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        show();//显示对话框


        setCanceledOnTouchOutside(true);
        setCancelable(true);
        if (minStopTime != 0) {
            minStopTimeTv.setText(String.valueOf(minStopTime));
            startTimer(minStopTime);
        } else {
            minStopTimeTv.setVisibility(View.GONE);
        }
    }

    public void display() {
        display(0);
    }

    RecyclerView.OnScrollListener onScrollListener;


    private void init() {
        submit_tv = (TextView) findViewById(R.id.submit_tv);
        receiveTv = (TextView) findViewById(R.id.receiveTv);
        maxTv = (TextView) findViewById(R.id.maxTv);
        minStopTimeTv = (TextView) findViewById(R.id.minStopTimeTv);
        setOnlistener();
    }

    private void setOnlistener() {
        submit_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSlectClickListener.submitClick(maxTv.getText().toString());
                dismiss();
            }
        });

    }

    public interface SlectClickListener {
        void submitClick(String value);
    }

    private void startTimer(final int minStopTime) {
        if (timerDisposable != null) {
            timerDisposable.dispose();
        }
        timerDisposable = Observable.interval(0, 1, TimeUnit.SECONDS).take(minStopTime + 1).map(new Function<Long, Long>() {
            @Override
            public Long apply(Long aLong) throws Exception {
                return minStopTime - aLong;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                minStopTimeTv.setText(String.valueOf(aLong));
            }
        });
    }


    String currentTime = Kits.Date.getYmdhmsS(Calendar.getInstance().getTimeInMillis());
    private void write(String content) {
        String fileName = "蓝牙RR" + currentTime + ".txt";
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String crashDir = Environment.getExternalStorageDirectory() + "/监测点系统日志/";
            Kits.File.writeFile(crashDir + fileName, "\n" + currentTime + ":" + content, true);
        }
    }


    private Float getLastValue() {
        String lastData = maxTv.getText().toString();
        Float lastValue = null;
        try {
            lastValue = Float.parseFloat(lastData);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return lastValue;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (!timerDisposable.isDisposed()) {
            timerDisposable.dispose();
        }
    }

    public void setReceiveDatas(String receiveData){
        receiveTv.setText(receiveData);
    }

}
